import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  //一级路由
  {
    name: 'interview',
    path: '/interview/:id/:city',
    component: () => import('@/views/discover/interview.vue')
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/corporate',
    name: 'corporate',
    component: () => import('../views/company/corporate.vue')
  },
  {
    name: 'layout',
    path: '/',
    redirect: '/exercise',
    component: () => import('@/views/layout.vue'),
    children: [
      {
        name: 'company',
        path: '/company',
        component: () => import('@/views/company/company.vue')
      },
      {
        name: 'discover',
        path: '/discover',
        component: () => import('@/views/discover/discover.vue')
      },
      {
        name: 'exercise',
        path: '/exercise',
        component: () => import('@/views/exercise/exercise.vue')
      },
      {
        name: 'mine',
        path: '/mine',
        component: () => import('@/views/mine/mine.vue')
      }
    ]
  },

  {
    name: 'particulars',
    path: '/particulars',
    component: () => import('@/views/exercise/particulars.vue')
  },
  //面试技巧列表
  {
    name: 'interviewing',
    path: '/interviewing',
    component: () => import('@/views/exercise/interviewing.vue')
  },
  //分享面经列表
  {
    name: 'Sharer',
    path: '/Sharer',
    component: () => import('@/views/exercise/Sharer.vue')
  },
  {
    name: 'search',
    path: '/search',
    component: () => import('@/views/exercise/search.vue')
  },
  // 我的页面之我的资料页面
  {
    name: 'mydata',
    path: '/mydata',
    component: () => import('@/views/mine/mydata.vue')
  },

  // 我的--修改岗位页面
  {
    name: 'station',
    path: '/station',
    component: () => import('@/views/mine/station.vue')
  },
  // 我的--修改头像页面
  {
    name: 'uploaderImg',
    path: '/uploaderImg',
    component: () => import('@/views/mine/uploaderImg.vue')
  },
  // 我的--修改昵称页面
  {
    name: 'editName',
    path: '/editName',
    component: () => import('@/views/mine/editName.vue')
  },
  // 我的--修改简介页面
  {
    name: 'editBriefing',
    path: '/editBriefing',
    component: () => import('@/views/mine/editBriefing.vue')
  }
]

//前置守卫（进入路由之前执行的逻辑代码）
// 申明一个白名单
const whiteList = [
  '/login',
  '/404',
  '/company',
  '/discover',
  '/exercise',
  '/particulars',
  '/interviewing'
]
//导入token
import { getToken } from '../utils/token'
// 导入vuex
import store from '../store'

import { Toast } from 'vant'
// 实例化路由对象
const router = new VueRouter({
  routes
})

router.beforeEach(async (to, from, next) => {
  //进入页面时开始判断 是否存在token  从localStorage取token
  if (getToken()) {
    const userInfo = store.state.userInfo
    // console.log(userInfo);
    //判断是否存在用户信息  严谨判断
    if (!userInfo || !userInfo.nickname) {
      //不存在：调用获取用户信息
      await store.dispatch('getUserInfo')
    }
    if (to.path === '/login') {
      // 判断是否为login页面，是跳转到首页
      Toast.fail('您已经登录，请不要调皮哦')
      next('/')
    } else {
      //不是，向后执行
      next()
    }
  } else {
    //不存在token
    if (whiteList.indexOf(to.path) !== -1) {
      //判断是否在白名单中
      next()
    } else {
      Toast.fail('请登录后访问~')
      //保存跳转登录页面前访问的路径
      // next(`/login?_redirect=${to.path}`)
      next(`/login?_redirect=${to.path}`)
      // console.log(to.path);
    }
  }
})

//后置
router.afterEach((to, from) => {
  // ...
})
export default router
